package org.micode.demo.db.dao;

import org.micode.demo.db.domain.dto.SysMenuDTO;
import org.micode.libs.dao.base.BaseAllDao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 菜单信息(sys_menu) 实体类的Dao
 *
 * author: wutianbin
 */
@Mapper
public interface SysMenuDao extends BaseAllDao<SysMenuDTO> {

    // 非常规sql写这里--- xml 自动生成,不再手动在xml中写sql !!
    @Select("select distinct rm.menu_id from sys_user_role ur \n" +
            "  LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id \n" +
            "  where ur.user_id = #{userId}")
    @ResultType(Long.class)
    List<Long> queryUserAllMenuId(@Param("userId") Long userId);

    @Select("SELECT * FROM sys_menu m " +
            " where menu_type != 2 " +
            " ORDER BY m.sort_no ")
    @ResultMap("sysMenuMap")
    List<SysMenuDTO> listAllNotButton();

    @Select("SELECT distinct * " +
            "  FROM sys_menu m " +
            "  INNER JOIN sys_role_menu rm on m.menu_id = rm.menu_id " +
            "  INNER JOIN sys_user_role ur on ur.role_id = rm.role_id " +
            " where m.menu_type != 2 " +
            " and ur.user_id = #{userId} " +
            " ORDER BY m.sort_no ")
    @ResultMap("sysMenuMap")
    List<SysMenuDTO> listUserMenuNotButton(@Param("userId") Long userId);

    @Select("select m.perms from sys_user_role ur\n" +
            "    LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id\n" +
            "    LEFT JOIN sys_menu m on rm.menu_id = m.menu_id\n" +
            "    where ur.user_id = #{userId}")
    @ResultType(String.class)
    Set<String> listPerms(@Param("userId") Long userId);

    @Select("select * " +
            " from sys_menu " +
            " where parent_id = #{parentId} " +
            " order by sort_no asc")
    @ResultMap("sysMenuMap")
    List<SysMenuDTO> queryListParentId(@Param("parentId") Long parentId);

}
